package zl.osimall.common.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zl.osimall.common.bean.Cart;
import zl.osimall.common.bean.Goods;
import zl.osimall.common.exception.SteduException;
import zl.osimall.common.mapper.CartMapper;
import zl.osimall.common.mapper.GoodsMapper;
import zl.osimall.common.service.CartService;

import java.util.List;
import java.util.Objects;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public boolean add(Cart cart) throws SteduException {
        Goods goods = goodsMapper.selectById(cart.getGoodsId());
        if(goods == null){
            throw new SteduException("该商品不存在");
        }
        if(goods.getStatus() == 1){
            throw new SteduException("该商品已下架");
        }
        Cart c = cartMapper.selectByUserIdAndGoodsId(cart.getUserId(), cart.getGoodsId());
        if(c != null){
            if(goods.getCount()<c.getCount() + 1){
                throw new SteduException("该商品不足");
            }
            return cartMapper.update(c.getId(), c.getCount()) == 1;
        }else {
            if (goods.getCount() == 0){
                throw new SteduException("该商品不足");
            }
            cart.setCount(1);
            return cartMapper.insert(cart) == 1;
        }
    }

    @Override
    public boolean delete(Integer id, Integer userId) throws SteduException {
        Cart cart = cartMapper.selectById(id);
        if(cart == null){
            throw new SteduException("该购物车信息不存在");
        }
        if(!Objects.equals(cart.getUserId(), userId)){
            throw new SteduException("该购物车信息不存在");
        }
        return cartMapper.delete(id)==1 ;
    }

    @Override
    public Boolean update(Integer cartId, Integer count, Integer userId) throws SteduException {
        Cart cart = cartMapper.selectById(cartId);
        if(cart == null){
            throw new SteduException("该购物车信息不存在");
        }
        if(!Objects.equals(cart.getUserId(), userId)){
            throw new SteduException("该购物车信息不存在");
        }
        if (cart.getGoods().getStatus() == 1){
            throw new SteduException("商品已下架");
        }
        if (cart.getGoods()==null || cart.getGoods().getCount()<count){
            throw new SteduException("该商品库存不足");
        }
        return cartMapper.update(cartId, count)==1 ;
    }

    @Override
    public List<Cart> selectByUserId(Integer userId) {
        return cartMapper.selectByUserId(userId);
    }

    @Override
    public List<Cart> selectByIds(Integer[] ids, Integer userId) throws SteduException {
        if(ids == null || ids.length == 0){
            throw new SteduException("请选择商品再进行结算");
        }
        List<Cart> cartList = cartMapper.selectByIds(ids);
        if(ids.length != cartList.size()){
            throw new SteduException("非法购物车信息，请确认后再结算");
        }
        for (Cart cart : cartList){
            if (cart.getUserId() != userId){
                throw new SteduException("非法购物车信息，请确认后再结算");
            }
        }
        return cartList  ;
    }
}
